using System;

namespace ICSharpCode.TextEditor.Formatting.Intellisense
{
    public class XmlCompletionData : DefaultCompletionData
    {
        private bool CompletionTagClosed = true;

        private bool SingleOneLine = false;

        public XmlCompletionData(string text, int imageIndex)
            : base(text, imageIndex)
        {
        }

        public XmlCompletionData(string text, string description, int imageIndex)
            : base(text, description, imageIndex)
        {
        }

        public XmlCompletionData(string text, string description, int imageIndex, bool SingleOneLine)
            : base(text, description, imageIndex)
        {
            this.SingleOneLine = SingleOneLine;
        }

        public override bool InsertAction(TextArea textArea, char ch)
        {
            string wordBeforeCaret = textArea.GetWordBeforeCaret();
            Caret caret = textArea.Caret;
            int num = caret.Offset + 1;
            int num2 = num;
            if (!string.IsNullOrEmpty(wordBeforeCaret))
            {
                num -= wordBeforeCaret.Length + 1;
                num2--;
                if (num2 - num > 0)
                {
                    textArea.Document.Remove(num, num2 - num);
                }
                textArea.Caret.Position = textArea.Document.OffsetToPosition(Math.Min(num, textArea.Document.TextLength));
            }
            string str = (text ?? "") ?? "";
            if (CompletionTagClosed)
            {
                str = ((!SingleOneLine) ? ((!(wordBeforeCaret == "<")) ? (text + "></" + text + ">") : ("<" + text + "></" + text + ">")) : ((!(wordBeforeCaret == "<")) ? (text + " />") : ("<" + text + " />")));
            }
            textArea.InsertString(str);
            return false;
        }

        public void CompletionTagClose(bool CompletionTagClosed)
        {
            this.CompletionTagClosed = CompletionTagClosed;
        }
    }
}
